(function(){
	var share = {
		init: function(){
			
			this.downloadUrl = "https://m.ued71.com/member/app_download.html"  // APP下载着陆页链接
			this.uedUrl = "https://m.ued71.com/"   // ued H5链接
			
			// ----------------------------------------------------------
			
			this.AndroidSchemeUrl = 'mued://mued.com/openVideo' // 安卓唤醒协议
	        this.iOSSchemeUrl = 'UShare://'  // iOS唤醒协议			

	        this.platform = this.getUrlParam('platform')
            this.type = this.getUrlParam('type')
            this.video_id = this.getUrlParam('video_id')
            this.u = this.getUrlParam('u')
            if(this.u){
            	this.u=this.u.split('ued_')[1]
            }
            
            this.jumph5regUrl = this.uedUrl + '/handle/reg.html?u='+this.u // h5注册页面链接
            this.jumph5videoUrl = this.uedUrl+"/video.html?type="+this.type+"&video_id="+this.video_id // h5视频页面链接

            this.shareStart()

		},
		shareStart: function() {
			if(this.is_WQ()){
				if(this.getDevice()=='iOS'){
					$('#iOS-wrap').show()
			    }else{
			    	$('#Android-wrap').show()
			    }
			    return 
			}
			switch(this.type){
				case 'download':
				  this.personalShare()
				  break;
				case 'register': 
				  this.myagentShare()
				  break;
				default:  
				  this.videoShare()
			}
		},
		// 个人中心分享
		personalShare: function() {
			window.location.href = this.downloadUrl
		},
		// 我的代理分享
		myagentShare: function() {
			if(this.platform=='h5'){
				window.location.href = this.jumph5regUrl
			}else{
				if(this.getDevice()=='iOS'){
					var schemeUrl = this.iOSSchemeUrl+"type="+this.type+"&username="+this.u
			    	this.openiOS(schemeUrl,this.jumph5regUrl)
			    }else{
			    	var schemeUrl = this.AndroidSchemeUrl+"?type="+this.type+"&username="+this.u
			    	this.openAndroid(schemeUrl,this.jumph5regUrl)
			    }
			}
		},
		// 视频分享
		videoShare: function() {
			if(this.platform=='h5'){
				window.location.href = this.jumph5videoUrl
			}else{
				if(this.getDevice()=='iOS'){
					var schemeUrl =  this.iOSSchemeUrl+"type="+this.type+"&video_id="+this.video_id
			    	this.openiOS(schemeUrl,this.jumph5videoUrl)
			    }else{
			    	var schemeUrl = this.AndroidSchemeUrl+"?type="+this.type+"&video_id="+this.video_id
			    	this.openAndroid(schemeUrl,this.jumph5videoUrl)
			    }
			}
		},
		//获取url中传递的参数值
		getUrlParam: function(name) {
			var url = window.location.href;
            var pos = url.indexOf("?");
            if (pos < 0) {
                return null;
            }
            var paramStr = url.substr(pos + 1);
            var ar = paramStr.split("&");
            if (ar.length == 0) {
                return null;
            }
            for (var i = 0, len = ar.length; i < len; i++) {
                var str = ar[i];
                var r = str.split("=");
                if (r.length != 2) {
                    continue;
                }
                if (r[0] == name) {
                    // 用decodeURI解码中文
                    return decodeURI(r[1]);
                }
            }
            return null;
		},
		// 判断是否是QQ或微信打开
		is_WQ: function() {
			var ua = navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger\/[0-9]/i)){
				return "weixin";
			}
			if(ua.match(/QQ\/[0-9]/i)){
				return "QQ";
			}
			return false
		},
		// 设备判断
		getDevice: function() {
			var u = navigator.userAgent;
			var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
            if(isAndroid){
            	return 'android'
            }else{
            	return 'iOS'
            }
		},
		// 唤醒Android的APP
		openAndroid: function(schemeUrl,jumpUrl) {
			window.location = schemeUrl
            this.notApp(jumpUrl);
		},
		// 唤醒iOS的APP
		openiOS: function(schemeUrl,jumpUrl) {
			window.location.href = schemeUrl
            this.notApp(jumpUrl);
		},
		// 没有下载APP
		notApp: function(jumpUrl) {
			var startTime = Date.now();
            var delay = setInterval(function(){
                var endTime = Date.now();
                if (!startTime || endTime - startTime < 2100) { 
                    window.location = jumpUrl;
                } 
                if(endTime-startTime>=3000){
                    clearInterval(delay);
                 }
            },1000);
		}
	}
	window.share = share
})()
share.init()